﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Text.RegularExpressions;

namespace Components
{
    public class Validation
    {
        public static string ConvertNullToEmpty(object o)
        {
            return IsNull(o) ? "" : o.ToString();
        }

        public static bool IsInt(string text)
        {
            try
            {
                int.Parse(text);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool IsFloat(string text)
        {
            try
            {
                decimal.Parse(text);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 检测是否是Float类型，且整数部分是否大于pcount的位数
        /// </summary>
        /// <param name="text"></param>
        /// <param name="pcount"></param>
        /// <returns></returns>
        public static bool IsFloat(string text, int pcount)
        {
            if (!IsFloat(text)) return false;

            int pointLocation = text.IndexOf('.');
            return text.Substring(0, (pointLocation == -1 ? text.Length : pointLocation)).Length <= pcount;
        }

        public static bool IsDouble(string text)
        {
            try
            {
                double.Parse(text);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static int ToInt(object v)
        {
            if (v == null || !Validation.IsInt(v.ToString())) return 0;

            return Convert.ToInt32(v);
        }

        public static decimal ToFloat(object v)
        {
            if (v == null || !Validation.IsFloat(v.ToString())) return 0;

            return Convert.ToDecimal(v);
        }

        public static Double ToDouble(object v)
        {
            if (v == null || !Validation.IsDouble(v.ToString())) return 0;

            return Convert.ToDouble(v);
        }

        /// <summary>
        /// 转换成Double类型
        /// </summary>
        /// <param name="v"></param>
        /// <param name="pcount">保留多少位小数</param>
        /// <returns></returns>
        public static Double ToDouble(object v, int pcount)
        {
            double tv = Validation.ToDouble(v);
            return Math.Round(tv, pcount, MidpointRounding.ToEven);
        }

        public static string ToString(object v)
        {
            if (Validation.IsNull(v)) return "";
            else if (v is bool) return Convert.ToBoolean(v) ? "1" : "0";
            return v.ToString();
        }

        public static bool IsNull(object v)
        {
            return (v is DBNull || v == null || "" == v.ToString());
        }

        public static bool ToBool(object v)
        {
            if (v == null) return false;
            else if (v is string) { return v.ToString() != "0" && v.ToString().ToLower() != "false"; }
            else if (v is bool) return Convert.ToBoolean(v);
            else if (v is int) return Convert.ToInt32(v) != 0;
            else return false;
        }

        public static bool IsDate(object v)
        {
            if (Validation.IsNull(v)) return false;

            try
            {
                DateTime.Parse(v.ToString());
                return v.ToString().Trim().Length < 11;
            }
            catch { return false; }
        }

        public static DateTime ToDate(object v)
        {
            try
            {
                return Convert.ToDateTime(v);
            }
            catch (Exception)
            {
                return DateTime.Now;
            }
        }

        public static double PercentToNumber(string value)
        {
            value = value.Replace("%", "");
            double dbValue = ToDouble(value);

            return (dbValue/100);
        }

        public static DateTime ToDate(object v, DateTime defaultValue)
        {
            try
            {
                return IsNull(v) ? defaultValue : Convert.ToDateTime(v);
            }
            catch (Exception)
            {
                return defaultValue;
            }
        }

        public static bool IsDateTime(object v)
        {
            if (IsNull(v)) return false;
            try
            {
                Convert.ToDateTime(v);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
